package com.fantasy.easy.biz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fantasy.easy.biz.domain.*;
import com.fantasy.easy.biz.domain.vo.DictVO;
import com.fantasy.easy.biz.domain.vo.VisualVO;
import com.fantasy.easy.biz.domain.vo.CountVO;
import com.fantasy.easy.biz.mapper.*;
import com.fantasy.easy.biz.service.BizBaseService;
import com.fantasy.easy.core.SessionSubjectUtils;
import com.fantasy.easy.core.entity.SysUserEntity;
import com.fantasy.easy.core.util.PBKDF2Util;
import com.fantasy.easy.sys.role.SysRoleEntity;
import com.fantasy.easy.sys.role.mapper.SysRoleMapper;
import com.fantasy.easy.sys.user.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 *
 */
@Service
public class BizBaseServiceImpl extends ServiceImpl<BizBaseMapper, BizBase>
        implements BizBaseService {

    @Resource
    BizBaseMapper bizBaseMapper;
    @Resource
    BizCollaMapper bizCollaMapper;
    @Resource
    BizCreditMapper bizCreditMapper;
    @Resource
    BizDataMapper bizDataMapper;
    @Resource
    BizEmployeeMapper bizEmployeeMapper;
    @Resource
    BizFeedbackMapper bizFeedbackMapper;
    @Resource
    SysUserMapper sysUserMapper;
    @Resource
    SysRoleMapper sysRoleMapper;

    @Override
    public IPage<BizBase> queryAll(BizBase bizBase, Page<BizBase> p) {
        return bizBaseMapper.selectPage(p, new QueryWrapper<>(bizBase));
    }

    @Override
    @Transactional
    public int addInfo(BizBase bizBase) {
        // 更新操作
        if (bizBase.getId() != 0) {
            return bizBaseMapper.updateById(bizBase);
        }
        List<SysUserEntity> sysUserEntities = sysUserMapper.selectList(new QueryWrapper<>());
        for (SysUserEntity sysUserEntity : sysUserEntities) {
            if (bizBase.getUname().equals(sysUserEntity.getName()) || sysUserEntity.getUserName().equals(bizBase.getUsername())) {
                return -1;
            }
        }
        bizBase.setId(null);
        // 基本信息表
        int res1 = bizBaseMapper.insert(bizBase);
        // 其它表占个位
        int res2 = bizCollaMapper.insertNull();
        int res3 = bizCreditMapper.insertNull();
        int res4 = bizDataMapper.insertNull();
        int res5 = bizFeedbackMapper.insertNull();
        // 同时存入系统用户维护
        SysUserEntity sysUserEntity = new SysUserEntity();
        sysUserEntity.setUserName(bizBase.getUsername());
        try {
            sysUserEntity.setUserSalt(PBKDF2Util.generateSalt()); // 生成随机盐值
            sysUserEntity.setUserPwd(PBKDF2Util.getEncryptedPassword(bizBase.getPassword(), sysUserEntity.getUserSalt())); // 加密密码
        } catch (Exception e) {
            e.printStackTrace();
        }
        sysUserEntity.setUpdateBy(SessionSubjectUtils.getUserId());
        sysUserEntity.setUpdateTime(new Date());
        sysUserEntity.setCreateBy(SessionSubjectUtils.getUserId());
        sysUserEntity.setCreateTime(new Date());
        sysUserEntity.setUserStatus(1);
        int res6 = sysUserMapper.insert(sysUserEntity);
        SysUserEntity userEntity = sysUserMapper.selectOne(sysUserEntity.getUserName());
        // 权限设置
        SysRoleEntity sysRoleEntity = new SysRoleEntity();
        ArrayList<Long> list = new ArrayList<>();
        list.add(userEntity.getUserId());
        sysRoleEntity.setUserIds(list);
        sysRoleEntity.setRoleId(3L);
        int res7 = sysRoleMapper.insertRoleUserByRole(sysRoleEntity);
        return res1 + res2 + res3 + res4 + res5 + res6 + res7;
    }

    @Override
    public int deleteInfo(Integer userId) {
        int res1 = bizBaseMapper.deleteById(userId);
        int res2 = bizCollaMapper.deleteById(userId);
        int res3 = bizCreditMapper.deleteById(userId);
        int res4 = bizDataMapper.deleteById(userId);
        int res5 = bizFeedbackMapper.deleteById(userId);
        return res1 + res2 + res3 + res4 + res5;
    }

    @Override
    public int getIdByName(String name) {
        BizBase base = new BizBase();
        base.setUsername(name);
        BizBase bizBase = bizBaseMapper.selectOne(new QueryWrapper<>(base));
        return bizBase.getId();
    }

    @Override
    public CountVO getCount() {
        int res1 = bizBaseMapper.selectCount(new QueryWrapper<>());
        int res2 = bizCollaMapper.selectCount(new QueryWrapper<>());
        int res3 = bizCreditMapper.selectCount(new QueryWrapper<>());
        int res4 = bizDataMapper.selectCount(new QueryWrapper<>());
        int res5 = bizFeedbackMapper.selectCount(new QueryWrapper<>());
        int res6 = bizEmployeeMapper.selectCount(new QueryWrapper<>());
        CountVO countVO = new CountVO();
        countVO.setBase(res1);
        countVO.setColla(res2);
        countVO.setCredit(res3);
        countVO.setData(res4);
        countVO.setFeedback(res5);
        countVO.setEmployee(res6);
        return countVO;
    }

    @Override
    public List<VisualVO> getValueAndNameWithColla() {
        List<BizBase> bizBases = bizBaseMapper.selectList(new QueryWrapper<>());
        List<BizColla> bizCollas = bizCollaMapper.selectList(new QueryWrapper<>());
        final int[] flag = new int[]{0};
        List<VisualVO> voList = bizBases.stream().map(item -> {
            int[] temp = new int[4];
            VisualVO vo = new VisualVO();
            vo.setName(item.getUname());
            if (flag[0] < bizCollas.size()) {
                BizColla i = bizCollas.get(flag[0]);
                temp[0] = Integer.parseInt(i.getHouse() == null ? "0" : i.getHouse());
                temp[1] = Integer.parseInt(i.getCar() == null ? "0" : i.getCar());
                temp[2] = Integer.parseInt(i.getStock() == null ? "0" : i.getStock());
                temp[3] = Integer.parseInt(i.getBond() == null ? "0" : i.getBond());
                vo.setValue(temp);
                flag[0]++;
            }
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public List<VisualVO> getValueAndNameWithCredit() {
        List<BizBase> bizBases = bizBaseMapper.selectList(new QueryWrapper<>());
        List<BizCredit> bizCredits = bizCreditMapper.selectList(new QueryWrapper<>());
        final int[] flag = new int[]{0};
        List<VisualVO> voList = bizBases.stream().map(item -> {
            int[] temp = new int[4];
            VisualVO vo = new VisualVO();
            vo.setName(item.getUname());
            if (flag[0] < bizCredits.size()) {
                BizCredit i = bizCredits.get(flag[0]);
                temp[0] = i.getCredit() == null ? 0 : i.getCredit();
                temp[1] = i.getHascrime() == null ? 0 : i.getHascrime();
                temp[2] = i.getIsaccused() == null ? 0 : i.getIsaccused();
                temp[3] = i.getHasdebt() == null ? 0 : i.getHasdebt();
                vo.setValue(temp);
                flag[0]++;
            }
            return vo;
        }).collect(Collectors.toList());
        return voList;
    }

    @Override
    public List<DictVO> queryAllNameDict() {
        List<BizBase> bizBases = bizBaseMapper.selectList(new QueryWrapper<>());
        return bizBases.stream().map(item -> {
            DictVO dictVO = new DictVO();
            BeanUtils.copyProperties(item, dictVO);
            return dictVO;
        }).collect(Collectors.toList());
    }
}




